package cn.itheima.action.takedelivery;

import com.opensymphony.xwork2.ActionContext;
import org.apache.commons.io.FileUtils;
import org.apache.struts2.ServletActionContext;
import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.Namespace;
import org.apache.struts2.convention.annotation.ParentPackage;
import org.apache.struts2.convention.annotation.Result;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;

import java.io.File;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.*;

/**
 * Created by millions on 2017/7/7.
 */
@ParentPackage("json-default")
@Namespace("/")
@Controller
@Scope("prototype")
public class ImageAction {

    private File imgFile;
    private String imgFileFileName;

    public void setImgFile(File imgFile) {
        this.imgFile = imgFile;
    }

    public void setImgFileFileName(String imgFileFileName) {
        this.imgFileFileName = imgFileFileName;
    }

    @Action(value = "image_upload",results = {@Result(type = "json")})
    public String uploadImage() throws IOException {

        System.out.println(imgFileFileName);

        //据对路径--用来存储
        String realPath = ServletActionContext.getServletContext().getRealPath("/upload");
        System.out.println("realPath:"+realPath);
        //相对路径--用来回显
        String returnUrl = ServletActionContext.getRequest().getContextPath() + "/upload";

        //生成随机的文件文名
        String randomFileName = UUID.randomUUID()+imgFileFileName.substring(imgFileFileName.lastIndexOf("."));

        //保存文件
        FileUtils.copyFile(imgFile,new File(realPath+"/"+randomFileName));

        //回写链接
        Map<String,Object> map=new HashMap<>();
        map.put("error",0);
        map.put("url",returnUrl+"/"+randomFileName);
        ActionContext.getContext().getValueStack().push(map);

        return "success";
    }

    @Action(value = "file_manager",results = {@Result(type = "json")})
    public String fileManager(){
        String rootPath = ServletActionContext.getServletContext().getRealPath(
                "/")
                + "upload/";

        String rootUrl = ServletActionContext.getRequest().getContextPath() + "/upload/";

        //图片后缀
        String[] fileTypes = new String[] { "gif", "jpg", "jpeg", "png", "bmp" };
        //根路径
        File currentPathFile = new File(rootPath);
        List<Map<String,Object>> fileList=new ArrayList<>();

        if (null != currentPathFile.listFiles()) {
            for(File file:currentPathFile.listFiles()){
                Map<String, Object> hash = new HashMap<>();
                String fileName = file.getName();
                if(file.isDirectory()) {
                    hash.put("is_dir", true);
                    hash.put("has_file", (file.listFiles() != null));
                    hash.put("filesize", 0L);
                    hash.put("is_photo", false);
                    hash.put("filetype", "");
                } else if(file.isFile()){
                    String fileExt = fileName.substring(fileName.lastIndexOf(".") + 1).toLowerCase();
                    hash.put("is_dir", false);
                    hash.put("has_file", false);
                    hash.put("filesize", file.length());
                    hash.put("is_photo", Arrays.<String>asList(fileTypes).contains(fileExt));
                    hash.put("filetype", fileExt);
                }
                hash.put("filename", fileName);
                hash.put("datetime", new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(file.lastModified()));
                fileList.add(hash);
            }
        }

        Map<String, Object> result = new HashMap<String, Object>();
        result.put("moveup_dir_path", "");
        result.put("current_dir_path", rootPath);
        result.put("current_url", rootUrl);
        result.put("total_count", fileList.size());
        result.put("file_list", fileList);

        ActionContext.getContext().getValueStack().push(result);

        return "success";
    }
}
